﻿using Common.Communal;
using ERP.Domain.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Repository.Interface;
using System.Text;
using System.Threading.Tasks;

namespace System.Services.Roles
{
    /// <summary>
    /// 角色服务接口
    /// </summary>
    public class RoleService:IRoleService
    {
        /// <summary>
        /// 角色仓储接口
        /// </summary>
        private readonly IRoleRepository roleRepository;
        /// <summary>
        /// 构造函数，注入角色仓储接口
        /// </summary>
        /// <param name="roleRepository">角色仓储接口</param>
        public RoleService(IRoleRepository roleRepository)
        {
            this.roleRepository = roleRepository;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Add(Role role)
        {
            var result = new ApiResult<int>();
            try
            {
                if (role == null)
                {
                    result.Error(result.Message = "角色信息不能为空");
                    return result;
                }
                if (string.IsNullOrWhiteSpace(role.RoleNo))
                {
                    result.Error(result.Message = "角色编号不能为空");
                    return result;
                }
                if (role.RoleNo.Length > 50)
                {
                    result.Error(result.Message = "角色编号长度不能超过50个字符");
                    return result;
                }
                if (string.IsNullOrWhiteSpace(role.RoleName))
                {
                    result.Error(result.Message = "角色名称不能为空");
                    return result;
                }
                if (role.RoleName.Length > 100)
                {
                    result.Error(result.Message = "角色名称长度不能超过100个字符");
                    return result;
                }
                if (!string.IsNullOrWhiteSpace(role.RoleDescription) && role.RoleDescription.Length > 500)
                {
                    result.Error(result.Message = "角色描述长度不能超过500个字符");
                    return result;
                }

                var no = await roleRepository.GetAsync(r => r.RoleNo == role.RoleNo && !r.IsDeleted);
                if (no.Count() > 0)
                {
                    result.Error(result.Message = "角色编号已存在");
                    return result;
                }

                var name = await roleRepository.GetAsync(r => r.RoleName == role.RoleName && !r.IsDeleted);
                if (name.Count() > 0)
                {
                    result.Error(result.Message = "角色名称已存在");
                    return result;
                }

                role.RoleStatus = true;
                role.IsDeleted = false;

                var res = await roleRepository.AddAsync(role);
                if (res > 0)
                {
                    result.Success(res, result.Message = "角色添加成功");
                }
                else
                {
                    result.Error(result.Message = "角色添加失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"角色添加异常: {ex.Message}");
            }
            return result;
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Update(Role role)
        {
            var result = new ApiResult<int>();
            try
            {
                if (role.RoleNo?.Length > 50)
                {
                    result.Error(result.Message = "角色编号长度不能超过50个字符");
                    return result;
                }
                if (role.RoleName?.Length > 100)
                {
                    result.Error(result.Message = "角色名称长度不能超过100个字符");
                    return result;
                }
                if (!string.IsNullOrWhiteSpace(role.RoleDescription) && role.RoleDescription.Length > 500)
                {
                    result.Error(result.Message = "角色描述长度不能超过500个字符");
                    return result;
                }
                var name = await roleRepository.GetAsync(r => r.RoleName == role.RoleName && r.Id != role.Id && !r.IsDeleted);
                if (name.Count() > 0)
                {
                    result.Error(result.Message = "角色名称已存在");
                    return result;
                }

                var res = await roleRepository.Update(role);
                if (res > 0)
                {
                    result.Success(res, result.Message = "角色更新成功");
                }
                else
                {
                    result.Error(result.Message = "角色更新失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"角色更新异常: {ex.Message}");
            }
            return result;
        }

        /// <summary>
        /// 逻辑删除角色
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Delete(int id)
        {
            var result = new ApiResult<int>();
            try
            {
                var role = await roleRepository.GetModelAsync(id);
                role.IsDeleted = true;
                var res = await roleRepository.Update(role);
                if (res > 0)
                {
                    result.Success(res, result.Message = "角色删除成功");
                }
                else
                {
                    result.Error(result.Message = "角色删除失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"角色删除异常: {ex.Message}");
            }
            return result;
        }

        /// <summary>
        /// 启用/禁用角色
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Enable(int id)
        {
            var result = new ApiResult<int>();
            try
            {
                var role = await roleRepository.GetModelAsync(id);
                role.RoleStatus = !role.RoleStatus;
                var res = await roleRepository.Update(role);
                if (res > 0)
                {
                    var statusText = role.RoleStatus ? "启用" : "禁用";
                    result.Success(res, result.Message = $"角色{statusText}成功");
                }
                else
                {
                    result.Error(result.Message = "角色状态更新失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"角色状态更新异常: {ex.Message}");
            }
            return result;
        }
    }
}
